package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.CheckBoxList
import com.glory.cqaospda.bean.PMStatusBean
import com.glory.cqaospda.utils.EditTextEnterUtils
import com.glory.cqaospda.viewmodel.PmModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.glorysoft.hlgdpda.utils.PopWindowsUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText
import kotlinx.android.synthetic.main.activity_pm.*
import kotlinx.android.synthetic.main.activity_title.*

class PmActivity : BaseActivity<PmModel>(), View.OnClickListener {

    var PMType = ""
    var idList = ArrayList<String>()
    private var codePop: PopWindowsUtils.GeneralPop? = null
    var checkBoxList = ArrayList<CheckBoxList>()
    var checkBoxListBean1 = CheckBoxList()
    var checkBoxListBean2 = CheckBoxList()
    var checkBoxListBean3 = CheckBoxList()
    var checkBoxListBean4 = CheckBoxList()
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@PmActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }
    var listQty = arrayListOf(
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20"
    )

    override fun initData() {

        queryEqpData(etEqpId)
        viewModel.defUI.callObserve.observe(this) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        val pmStatusBean = it.item as PMStatusBean

                        when (pmStatusBean!!.pmtype) {
                            "" -> {
                                tvBaseSubmit.text = "预约PM"
                            }
                            "null" -> {
                                tvBaseSubmit.text = "预约PM"
                            }
                            "PM_Prepare" -> {
                                tvBaseSubmit.text = "开始PM"
                            }
                            "PM_Start" -> {
                                tvBaseSubmit.text = "结束PM"
                                lineMaintainVerification.visibility = View.VISIBLE
                            }
                            "PM_End" -> {
                                tvBaseSubmit.text = "预约PM"
                            }
                        }
                    }
                    2 -> {
                        viewModel.defUI.toastEvent.value = ToastMessageBean("成功", true)
                        finish()
//                    viewModel.defaultGetPmEqpStatus(etEqpId.text.toString())
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }

        flBuyoffType.setOnClickListener {
            codePop = PopWindowsUtils.getOneChoosePop(this, flBuyoffType.width, listQty,
                ivBuyoffTypeArrow, object : PopWindowsUtils.PopTextAndPositionCallback {
                    override fun onSuccess(string: String, potion: Int) {
                        tvBuyoffType.text = string
                        checkBoxListBean1.maintenanceValue = tvBuyoffType.text.toString()
                    }
                }
            )
            if (codePop!!.isShowing) {
                codePop?.dismiss()
            } else {
                PopWindowsUtils.setArrowShow(ivBuyoffTypeArrow)
                codePop?.showAsDropDown(flBuyoffType, 0, 0)
            }
        }
        flQuanType.setOnClickListener {
            codePop = PopWindowsUtils.getOneChoosePop(this, flQuanType.width, listQty,
                ivQuanTypeArrow, object : PopWindowsUtils.PopTextAndPositionCallback {
                    override fun onSuccess(string: String, potion: Int) {
                        tvQuanType.text = string
                        checkBoxListBean2.maintenanceValue = tvQuanType.text.toString()
                    }
                }
            )
            if (codePop!!.isShowing) {
                codePop?.dismiss()
            } else {
                PopWindowsUtils.setArrowShow(ivQuanTypeArrow)
                codePop?.showAsDropDown(flQuanType, 0, 0)
            }
        }
        flQAType.setOnClickListener {
            codePop = PopWindowsUtils.getOneChoosePop(this, flQAType.width, listQty,
                ivQATypeArrow, object : PopWindowsUtils.PopTextAndPositionCallback {
                    override fun onSuccess(string: String, potion: Int) {
                        tvQAType.text = string
                        checkBoxListBean3.maintenanceValue = tvQAType.text.toString()
                    }
                }
            )
            if (codePop!!.isShowing) {
                codePop?.dismiss()
            } else {
                PopWindowsUtils.setArrowShow(ivQATypeArrow)
                codePop?.showAsDropDown(flQAType, 0, 0)
            }
        }
        flpeelingType.setOnClickListener {
            codePop = PopWindowsUtils.getOneChoosePop(this, flpeelingType.width, listQty,
                ivpeelingTypeArrow, object : PopWindowsUtils.PopTextAndPositionCallback {
                    override fun onSuccess(string: String, potion: Int) {
                        tvpeelingType.text = string
                        checkBoxListBean4.maintenanceValue = tvpeelingType.text.toString()
                    }
                }
            )
            if (codePop!!.isShowing) {
                codePop?.dismiss()
            } else {
                PopWindowsUtils.setArrowShow(ivpeelingTypeArrow)
                codePop?.showAsDropDown(flpeelingType, 0, 0)
            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
//        tvBaseSubmit.visibility = View.GONE
        tvTitle.text = "PM"
        tvBaseSubmit.text = "预约PM"
        checkBoxListBean1.maintenanceKey = "0"
        checkBoxListBean1.maintenanceValue = tvBuyoffType.text.toString()
        checkBoxList.add(checkBoxListBean1)
        tvUserName.text = Constant.UserName
        FreeApi_ActivityManager.getActivityManager().addActivity(this)

        val instance = MyApp.instance
        instance!!.handler = handler
        cbMaintainVerification0.setOnClickListener(this)
        cbMaintainVerification1.setOnClickListener(this)
        cbMaintainVerification2.setOnClickListener(this)
        cbMaintainVerification3.setOnClickListener(this)
        cbMaintainVerification4.setOnClickListener(this)

        tvBaseSubmit.setOnClickListener {
            if (Constant.isFastClick()) {
                when (tvBaseSubmit.text.toString()) {
                    "预约PM" -> {
                        PMType = "PM_Prepare"
                    }
                    "开始PM" -> {
                        PMType = "PM_Start"
                    }
                    "结束PM" -> {
                        PMType = "PM_End"
                    }
                }

                var pMValidation = ""
                if (checkBoxList.size > 0) {
                    for (s in checkBoxList) {
                        pMValidation = "$pMValidation,$s"
                    }
                } else {
                    pMValidation = ""
                }

                val map = JsonObject()
                map.addProperty("EquipmentName", etEqpId.text.toString())
                map.addProperty("LotNo", etLotId.text.toString())
                map.addProperty("PMType", PMType)
                map.addProperty("PMValidation", pMValidation)

                if (tvBaseSubmit.text.toString() == "结束PM") {
                    map.add(
                        "MaintenanceItemList",
                        JsonParser().parse(Gson().toJson(checkBoxList)).asJsonArray
                    )
                }
                viewModel.defaultPM(map)
            }
        }

    }

    override fun layoutId(): Int {
        return R.layout.activity_pm
    }

    private fun queryEqpData(
        editText: FreeUI_ClearEditText?,
    ) {
        EditTextEnterUtils.initEnterToUpperCase(editText)
        FreeApi_EditTextUtils.setEditTextListener(editText, this) { s: String? ->
            viewModel.defaultGetPmEqpStatus(s!!)
        }
    }

    override fun onClick(view: View?) {
        when (view!!.id) {
            R.id.cbMaintainVerification0 -> {
                if (cbMaintainVerification0.isChecked) {
                    if (tvBaseSubmit.text.toString() == "结束PM") {
                        flBuyoffType.visibility = View.VISIBLE
                    }
                    checkBoxListBean1.maintenanceKey = "0"
                    checkBoxListBean1.maintenanceValue = tvBuyoffType.text.toString()
                    checkBoxList.add(checkBoxListBean1)
                } else {
                    flBuyoffType.visibility = View.GONE
                    checkBoxList.remove(checkBoxListBean1)
                }
            }
            R.id.cbMaintainVerification1 -> {
                if (cbMaintainVerification1.isChecked) {
                    if (tvBaseSubmit.text.toString() == "结束PM") {
                        flQuanType.visibility = View.VISIBLE
                    }
                    checkBoxListBean2.maintenanceKey = "1"
                    checkBoxListBean2.maintenanceValue = tvQuanType.text.toString()
                    checkBoxList.add(checkBoxListBean2)
                } else {
                    flQuanType.visibility = View.GONE
                    checkBoxList.remove(checkBoxListBean2)
                }
            }
            R.id.cbMaintainVerification2 -> {
                if (cbMaintainVerification2.isChecked) {
                    if (tvBaseSubmit.text.toString() == "结束PM") {
                        flQAType.visibility = View.VISIBLE
                    }
                    checkBoxListBean3.maintenanceKey = "2"
                    checkBoxListBean3.maintenanceValue = tvQAType.text.toString()
                    checkBoxList.add(checkBoxListBean3)
                } else {
                    flQAType.visibility = View.GONE
                    checkBoxList.remove(checkBoxListBean3)
                }
            }
            R.id.cbMaintainVerification3 -> {
                if (cbMaintainVerification3.isChecked) {
                    if (tvBaseSubmit.text.toString() == "结束PM") {
                        flpeelingType.visibility = View.VISIBLE
                    }
                    checkBoxListBean4.maintenanceKey = "3"
                    checkBoxListBean4.maintenanceValue = tvpeelingType.text.toString()
                    checkBoxList.add(checkBoxListBean4)
                } else {
                    flpeelingType.visibility = View.GONE
                    checkBoxList.remove(checkBoxListBean4)
                }
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        if (keyCode == 520) {
            try {
                val view = window.decorView.findFocus() as EditText
                when (view) {
                    etLotId -> {
                        etLotId.setText("")
                    }
                    etEqpId -> {
                        etEqpId.setText("")
                    }
                }
            } catch (e: Exception) {
                println(e.message)
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }
}